const zlib = require("node:zlib");
const fs = require("node:fs");
const ht = require("node:http");
//gzip deflate
//三行代码压缩 gzip
// const readStream = fs.createReadStream("index.txt");
// const writeStream = fs.createWriteStream("index.txt.gz"); //后缀是.gz
// //pipe 管道用来处理流 先读取再写入
// readStream.pipe(zlib.createGzip()).pipe(writeStream); //压缩前60k 压缩后250 bytes
//三行代码解压gzip
// const readStream = fs.createReadStream("index.txt.gz");
// const writeStream = fs.createWriteStream("index2.txt"); //后缀是.gz
// readStream.pipe(zlib.createGunzip()).pipe(writeStream); //解压前250 bytes 解压后60k
//deflate
//三行代码压缩 deflate
// const readStream = fs.createReadStream("index.txt");
// const writeStream = fs.createWriteStream("index.txt.deflate"); //后缀是.deflate
// readStream.pipe(zlib.createDeflate()).pipe(writeStream); //压缩前60k 压缩后238 bytes
//三行代码解压deflate
// const readStream = fs.createReadStream("index.txt.deflate");
// const writeStream = fs.createWriteStream("index3.txt"); //后缀是.gz
// readStream.pipe(zlib.createInflate()).pipe(writeStream); //解压前238 bytes 解压后60k
// gzip 3.2kb->229bytes 适用于压缩文件 无损
// const server = ht.createServer((req, res) => {
//   const txt = "cqj".repeat(1000);
//   res.setHeader("Content-Encoding", "gzip");
//   res.setHeader("Content-Type", "text/plain;charset=utf8");
//   let result = zlib.gzipSync(txt);
//   res.end(result);
// });
// deflate 3.2kb->220bytes 适用于压缩http里的内容
const server = ht.createServer((req, res) => {
  const txt = "cqj".repeat(1000);
  res.setHeader("Content-Encoding", "deflate");
  res.setHeader("Content-Type", "text/plain;charset=utf8");
  let result = zlib.deflateSync(txt);
  res.end(result);
});
//端口号小于65535
server.listen(3000, () => {
  console.log("server is running");
});
